#pragma once

#include "SandMap.h"

enum DrawType {
	Erase = TileType::Empty,
	Sandd = TileType::Sand,
	Rockk = TileType::Rock,
	Waterr = TileType::Water,
};

class Painter : public sf::Drawable {
public:
	Painter(SandMap& map) :map(map), brush(map.tileunit) {}

	void update(const sf::Vector2i& curpos, bool ispressing) {
		// update brush
		sf::Vector2f centroid = {
			(float)curpos.x ,
			(float)curpos.y
		};
		brush.setpos(centroid, (int)scaler);
		brush.settype((TileType)type);

		// update map
		if (ispressing) {
			int mapx = curpos.x / map.tileunit;
			int mapy = curpos.y / map.tileunit;
			map.setmap(mapx, mapy, (TileType)type, (int)scaler);
		}
	}

	void set_brush(DrawType dtype) {
		type = dtype;
		brush.settype((TileType)type);
	}

	void scale_brush(float scale) {
		scaler += scale;
		scaler = std::clamp(scaler, minscaler, maxscaler);
	}

private:
	DrawType type = DrawType::Sandd;
	SandMap& map;
	float scaler = 1.0f;
	const float minscaler = 1.0f, maxscaler = 10.0f;
	mutable Pixel brush;
	virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
		target.draw(brush);
	}
};
